import axios from 'axios';
import { ElLoading, ElMessage } from 'element-plus'

import { buildUrl, baseUrl } from '@/config'
// import router from '@/router/index'

// 开始和结束加载动画函数
let loading;
const startLoading = () => {
    loading = ElLoading.service({
        lock: true,
        text: '加载中...',
        background: 'rgba(0,0,0,0.7)'
    });
}
const endLoading = () => {
    loading.close();
}
// const apis = {
//     // production: 'http://fenglingyun.top:6000', // 线上 (生成环境)
//     production: 'http://fenglingyun.top', // 线上 (生成环境)
//     development: 'http://localhost:5000', // 本地 (开发环境)
// }
// let baseURL1 = ''
// if (process.env.NODE_ENV === 'production') {// 生产环境
//     baseURL1 = apis.production
// } else {// 本地服务
//     baseURL1 = apis.development
// }
console.log('@环境', process.env.NODE_ENV);

const instance = axios.create({
    // baseURL: process.env.NODE_ENV === 'production'? 'http://fenglingyun.top': 'http://localhost:5000',
    // baseURL: process.env.NODE_ENV === 'development'? '/': 'http://fenglingyun.top',
    // baseURL: process.env.NODE_ENV === 'development'? '/': 'http://39.103.152.237:8077',  // 测试
    baseURL: process.env.NODE_ENV === 'development'? '/': baseUrl, // 正式
    timeout: 20000,
})
// console.log('@PPP', instance.axios.baseURL);
// 请求拦截
instance.interceptors.request.use(config => {
    // 加载动画
    startLoading();

    // 有token的需要在请求头加上token
    if (localStorage.eleToken) {
        // config.headers.Authorization = localStorage.eleToken;
        config.headers.Authorization = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYzMDM5NTBiOGRiMWExNDhkZDQxZTA0NCIsIm5hbWUiOiLmm7noiq7lvawiLCJhdmF0YXIiOiIvL3d3dy5ncmF2YXRhci5jb20vYXZhdGFyLzE0ZDkyMWY1Y2IwMThhODRlZTk2MmJjMWY0YmExZjgzP3M9MjAwJnI9cGcmZD1tbSIsImlkZW50aXR5IjoibWFuYWdlciIsImlhdCI6MTcwNTgxMTcyMSwiZXhwIjoxNzA1ODE1MzIxfQ.i_sqCCm1d2GUFZRuK_Fn3PAU6ogS4rgdgL6e6FZGbDE';

    }
    return config;
}, error => {
    return Promise.reject(error)
})

// 响应拦截
instance.interceptors.response.use(response => {
    // 结束加载动画
    endLoading();
    return response;
}, error => {
    endLoading();

    //获取错误状态码
    // const { status } = error.response;
    // 401跳转登录页面
    // if (status == 401) {
    //     ElMessage.error('Token失效，请重新登录！');
    //     // 清除过期token
    //     localStorage.removeItem('eleToken');
    //     // 跳转登录页面
    //     router.push("/login");
    // } else {
    //     ElMessage.error(error.response.data);
    // }
    ElMessage.error(error.response.data);

    return Promise.reject(error);
})

export const getRequest = (url = '', params = {}, config = {}) => {
    return instance({
        method: 'get',
        url,
        params,
        ...config
    }).then(response => {
        return response
    })
}

export const postRequest = (url = '', data = {}, config = {}) => {
    return instance({
        method: 'post',
        url,
        data,
        ...config
    }).then(response => {
        return response
    })
}

export const deleteRequest = (url = '', data = {}, config = {}) => {
    return instance({
        method: 'delete',
        url,
        data,
        ...config
    }).then(response => {
        return response
    })
}